#include "core.h"

CSearch::CSearch()
{
	Initialize("");
}

CSearch::~CSearch()
{

}

CSearch* CSearch::getInstance()
{
	CSearch *p = NULL;
	if (!p)
	{
		p = new CSearch;
	}
	return p;
}

bool CSearch::Initialize(std::string host)
{
	m_strUrl = "http://";
	m_strUrl += host;
	m_strUrl += "/search";
	return true;
}

bool CSearch::Destory()
{
	delete this;
	return true;
}

STSearchResponse* CSearch::search(STSearchRequest *pRequest)
{
	std::string requestUrl = m_strUrl;
	m_mapRequestParameter = mapRequestParameter;
	m_vecRequestParameter = vecRequestParameter;
	addQuerryParameter("query", pRequest->query, m_mapRequestParameter, m_vecRequestParameter);
	addQuerryParameter("index_name", pRequest->index_name, m_mapRequestParameter, m_vecRequestParameter);
	addQuerryParameter("fetch_fields", pRequest->fetch_fields, m_mapRequestParameter, m_vecRequestParameter);
	addQuerryParameter("qp", pRequest->qp, m_mapRequestParameter, m_vecRequestParameter);
	addQuerryParameter("disable", pRequest->disable, m_mapRequestParameter, m_vecRequestParameter);
	addQuerryParameter("first_formula_name", pRequest->first_formula_name, m_mapRequestParameter, m_vecRequestParameter);
	addQuerryParameter("formula_name", pRequest->formula_name, m_mapRequestParameter, m_vecRequestParameter);
	addQuerryParameter("summary", pRequest->summary, m_mapRequestParameter, m_vecRequestParameter);

	std::string strtmp = "";
	if (!Request(requestUrl, "GET", GetQuerryParameter(m_mapRequestParameter, m_vecRequestParameter, "GET", strtmp)))
	{
		printf("[%s]:%d\n", __FUNCTION__, __LINE__);
		return NULL;
	}

	searchResponse();
	return &m_searchResponse;
}
bool CSearch::searchResponse()
{
	Json::Reader reader;
	Json::Value result;
	printf("%s\n", g_buf_tmp.data);
	if (reader.parse(g_buf_tmp.data, result))
	{
		m_searchResponse.status = result["status"].asString();
		if (m_searchResponse.status == "OK")
		{
			Json::Value result2 = result["result"];
			m_searchResponse.searchtime = result2["searchtime"].asDouble();
			m_searchResponse.total = result2["total"].asUInt();
			m_searchResponse.num = result2["num"].asUInt();
			m_searchResponse.viewtotal = result2["viewtotal"].asUInt();
			m_searchResponse.items = result2["items"].asString();
			m_searchResponse.facet = result2["facet"].asString();
		}
		else
		{
			uint32_t count = result["errors"].size();
			Json::Value errors = result["errors"];
			for (uint32_t i=0; i<count; i++)
			{
				STErrorInfo errorInfo;
				errorInfo.errorCode = errors[i]["code"].asUInt();
				errorInfo.errorMessage = errors[i]["message"].asString();
				m_searchResponse.errorList.push_back(errorInfo);
			}
		}
		m_searchResponse.request_id = result["request_id"].asString();
	}
	return true;
}

STScanResponse* CSearch::scan(STScanRequest *pRequest)
{
	std::string requestUrl = m_strUrl;
	m_mapRequestParameter = mapRequestParameter;
	m_vecRequestParameter = vecRequestParameter;
	addQuerryParameter("scroll", pRequest->scroll, m_mapRequestParameter, m_vecRequestParameter);
	addQuerryParameter("search_type", pRequest->search_type, m_mapRequestParameter, m_vecRequestParameter);
	addQuerryParameter("index_name", pRequest->index_name, m_mapRequestParameter, m_vecRequestParameter);
	addQuerryParameter("query", pRequest->query, m_mapRequestParameter, m_vecRequestParameter);

	std::string strtmp = "";
	if (!Request(requestUrl, "GET", GetQuerryParameter(m_mapRequestParameter, m_vecRequestParameter, "GET", strtmp)))
	{
		printf("[%s]:%d\n", __FUNCTION__, __LINE__);
		return NULL;
	}

	scanResponse();
	return &m_scanResponse;
}

bool CSearch::scanResponse()
{
	Json::Reader reader;
	Json::Value result;
	printf("%s\n", g_buf_tmp.data);
	if (reader.parse(g_buf_tmp.data, result))
	{
		m_scanResponse.status = result["status"].asString();
		if (m_scanResponse.status == "OK")
		{
			Json::Value result2 = result["result"];
			m_scanResponse.searchtime = result2["searchtime"].asDouble();
			m_scanResponse.total = result2["total"].asUInt();
			m_scanResponse.num = result2["num"].asUInt();
			m_scanResponse.viewtotal = result2["viewtotal"].asUInt();
			m_scanResponse.scroll_id = result2["scroll_id"].asString();
			m_scanResponse.items = result2["items"].asString();
			m_scanResponse.facet = result2["facet"].asString();
		}
		else
		{
			uint32_t count = result["errors"].size();
			Json::Value errors = result["errors"];
			for (uint32_t i=0; i<count; i++)
			{
				STErrorInfo errorInfo;
				errorInfo.errorCode = errors[i]["code"].asUInt();
				errorInfo.errorMessage = errors[i]["message"].asString();
				m_scanResponse.errorList.push_back(errorInfo);
			}
		}
		m_scanResponse.request_id = result["request_id"].asString();
	}
	return true;
}